//
//  LoginUserRegistController.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/8/9.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoginUserRegistController.h"


@implementation LoginUserRegistController

-(instancetype)init{
    
    if(self = [super init]){
        
        
    }
    return self;
    
}

- (void)loadView{
    [super loadView];
}


-(void)viewDidLoad{
    [super viewDidLoad];
    [self.loginMainView hiddenExtraViewWith:@"注册"];
    [self addLeftBackButton];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)addLeftBackButton{
    UIButton *backButton = [self addLeftBackButtonForNoneNavi];
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(20);
        make.left.equalTo(self.view.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(40, 60));
    }];
    [backButton addTarget:self action:@selector(backBtnPressed ) forControlEvents:UIControlEventTouchUpInside];
}
- (void)backBtnPressed{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

//获取验证码
- (void)verifyBtnPressed:(id)sender {
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString * phone = cell.userInput.text;
    if (phone.length <= 0) {
        [self showToastAddWithTitle:@"手机号码不能为空"];
        return;
    }
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    [self.loginMainAction registerForProvingWithPhoneNumber:phone withCallBack:^(id error_msg) {
        [LoadingAnimalView hideLoadingForView:self.view];
        if (error_msg) {
           
            [self showToastAddWithTitle:error_msg];
        }else {
            
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerWithAction) userInfo:nil repeats:YES];
        }
    }];
}


//注册
- (void)loginBtnPressed:(id)sender {
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString *phone = cell.userInput.text;
    indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString *checksms = cell.userInput.text;
    indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString *passwd = cell.userInput.text;
    indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString *newpasswd = cell.userInput.text;
    if (phone.length <= 0) {
        [self showToastAddWithTitle:@"手机号不能为空"];
        return;
    }else if (checksms.length <= 0) {
        [self showToastAddWithTitle:@"验证码不能为空" ];
        return;
    }else if (passwd.length <= 0) {
        [self showToastAddWithTitle:@"密码不能为空"];
        return;
    }else if (newpasswd.length <= 0) {
        [self showToastAddWithTitle:@"确认密码不能为空"];
        return;
    }else if (passwd.length < 6 || passwd.length >16) {
        [self showToastAddWithTitle:@"密码长度为6到16个字符"];
        return;
    }else if (![passwd isEqualToString:newpasswd]){
        [self showToastAddWithTitle:@"密码和确认密码不等"];
        return;
    }
    
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    [self.loginMainAction registerForPhoneNumber:phone withChecksms:checksms withPasswd:passwd withRegisterPasswd:newpasswd withCallBack:^(id error_msg) {
        [LoadingAnimalView hideLoadingForView:self.view];
        if (error_msg) {
            [self showToastAddWithTitle:error_msg];
        }else {
            if (goBack) {
                [loginMainAction gorGoBackToMeViewController];
            }
          if (self.isRn) {
              UIApplication *application = [UIApplication sharedApplication];
              UIWindow *window = application.keyWindow;
              
              
              
              UIViewController * rootViewC= window.rootViewController;
              [rootViewC.navigationController popViewControllerAnimated:YES];
//            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
////            [appDelegate.preController.navigationController popViewControllerAnimated:YES];
//            [appDelegate.preController.navigationController popToRootViewControllerAnimated:YES];

          }else{
            [self dismissViewControllerAnimated:YES completion:nil];
          }

        }
    }];
}



@end
